home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / ztype.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-08  |  11.7 KB  |  464 lines

  1. /* Copyright (C) 1989, 1995, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* ztype.c */
  20. /* Type, attribute, and conversion operators */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "string_.h"
  24. #include "ghost.h"
  25. #include "errors.h"
  26. #include "oper.h"
  27. #include "imemory.h"            /* for register_ref_root */
  28. #include "idict.h"
  29. #include "iname.h"
  30. #include "stream.h"            /* for iscan.h */
  31. #include "strimpl.h"            /* for sfilter.h for picky compilers */
  32. #include "sfilter.h"            /* ditto */
  33. #include "iscan.h"
  34. #include "iutil.h"
  35. #include "dstack.h"            /* for dict_set_top */
  36. #include "store.h"
  37.  
  38. /* Forward references */
  39. private int near access_check(P3(os_ptr, int, bool));
  40. private int convert_to_string(P2(os_ptr, os_ptr));
  41.  
  42. /*
  43.  * Max and min integer values expressed as reals.
  44.  * Note that these are biased by 1 to allow for truncation.
  45.  * They should be #defines rather than static consts, but
  46.  * several of the SCO Unix compilers apparently can't handle this.
  47.  * On the other hand, the DEC compiler can't handle casts in
  48.  * constant expressions, so we can't use min_long and max_long.
  49.  * What a nuisance!
  50.  */
  51. #define alt_min_long (-1L << (arch_sizeof_long * 8 - 1))
  52. #define alt_max_long (~(alt_min_long))
  53. private const double min_int_real = (alt_min_long * 1.0 - 1);
  54. private const double max_int_real = (alt_max_long * 1.0 + 1);
  55. #define real_can_be_int(v)\
  56.   ((v) > min_int_real && (v) < max_int_real)
  57. #define zcvi_possible(v) real_can_be_int(v)
  58.  
  59. /* Get the pointer to the access flags for a ref. */
  60. #define access_ref(opp)\
  61.   (r_has_type(opp, t_dictionary) ? dict_access_ref(opp) : opp)
  62.  
  63. /* Initialize the table of type names. */
  64. /* We export the type names just in case they might be useful. */
  65. ref type_names;            /* t_array */
  66. private ref *type_names_p = &type_names;
  67. private gs_gc_root_t type_names_root;
  68. private void
  69. ztype_init(void)
  70. {    static const char _ds *tnames[] = { type_name_strings };
  71.     int i;
  72.     ialloc_ref_array(&type_names, a_readonly, t_next_index,
  73.              "type names");
  74.     for ( i = 0; i < t_next_index; i++ )
  75.     {    if ( tnames[i] == 0 )
  76.           make_null(&type_names.value.refs[i]);
  77.         else
  78.           {    name_enter_string(tnames[i],
  79.                       &type_names.value.refs[i]);
  80.             r_set_attrs(&type_names.value.refs[i], a_executable);
  81.           }
  82.     }
  83.     gs_register_ref_root(imemory, &type_names_root,
  84.                  (void **)&type_names_p, "type_names");
  85. }
  86.  
  87. /* <obj> type <name> */
  88. private int
  89. ztype(register os_ptr op)
  90. {    ref *ptref = &type_names.value.refs[r_btype(op)];
  91.     if ( !r_has_type(ptref, t_name) )
  92.     {    /* Must be either a stack underflow or a t_[a]struct. */
  93.         check_op(1);
  94.       {    /* Get the type name from the structure. */
  95.         const char *sname =
  96.           gs_struct_type_name_string(gs_object_type(imemory,
  97.                               op->value.pstruct));
  98.         int code = name_ref((const byte *)sname, strlen(sname),
  99.                     (ref *)op, 0);
  100.         if ( code < 0 )
  101.             return code;
  102.       }
  103.         r_set_attrs(op, a_executable);
  104.     }
  105.     else
  106.     {    ref_assign(op, ptref);
  107.     }
  108.     return 0;
  109. }
  110.  
  111. /* <obj> cvlit <obj> */
  112. private int
  113. zcvlit(register os_ptr op)
  114. {    ref *aop;
  115.     check_op(1);
  116.     aop = access_ref(op);
  117.     r_clear_attrs(aop, a_executable);
  118.     return 0;
  119. }
  120.  
  121. /* <obj> cvx <obj> */
  122. int
  123. zcvx(register os_ptr op)
  124. {    ref *aop;
  125.     uint opidx;
  126.     check_op(1);
  127.     /*
  128.      * If the object is an internal operator, we can't allow it to
  129.      * exist in executable form anywhere outside the e-stack.
  130.      */
  131.     if ( r_has_type(op, t_operator) &&
  132.          ((opidx = op_index(op)) == 0 ||
  133.           op_def_is_internal(op_def_table[opidx]))
  134.        )
  135.       return_error(e_rangecheck);
  136.     aop = access_ref(op);
  137.     r_set_attrs(aop, a_executable);
  138.     return 0;
  139. }
  140.  
  141. /* <obj> xcheck <bool> */
  142. private int
  143. zxcheck(register os_ptr op)
  144. {    check_op(1);
  145.     make_bool(op, (r_has_attr(access_ref(op), a_executable) ? 1 : 0));
  146.     return 0;
  147. }
  148.  
  149. /* <obj:array|packedarray|file|string> executeonly <obj> */
  150. private int
  151. zexecuteonly(register os_ptr op)
  152. {    check_op(1);
  153.     if ( r_has_type(op, t_dictionary) )
  154.       return_error(e_typecheck);
  155.     return access_check(op, a_execute, true);
  156. }
  157.  
  158. /* <obj:array|packedarray|dict|file|string> noaccess <obj> */
  159. private int
  160. znoaccess(register os_ptr op)
  161. {    check_op(1);
  162.     if ( r_has_type(op, t_dictionary) )
  163.       { /*
  164.          * Setting noaccess on a read-only dictionary is an attempt to
  165.          * change its value, which is forbidden (this is a subtle
  166.          * point confirmed with Adobe).  Also, don't allow removing
  167.          * read access to permanent dictionaries.
  168.          */
  169.         if ( dict_is_permanent_on_dstack(op) ||
  170.          !r_has_attr(dict_access_ref(op), a_write)
  171.            )
  172.           return_error(e_invalidaccess);
  173.       }
  174.     return access_check(op, 0, true);
  175. }
  176.  
  177. /* <obj:array|packedarray|dict|file|string> readonly <obj> */
  178. int
  179. zreadonly(register os_ptr op)
  180. {    return access_check(op, a_readonly, true);
  181. }
  182.  
  183. /* <array|packedarray|dict|file|string> rcheck <bool> */
  184. private int
  185. zrcheck(register os_ptr op)
  186. {    int code = access_check(op, a_read, false);
  187.     if ( code >= 0 ) make_bool(op, code), code = 0;
  188.     return code;
  189. }
  190.  
  191. /* <array|packedarray|dict|file|string> wcheck <bool> */
  192. private int
  193. zwcheck(register os_ptr op)
  194. {    int code = access_check(op, a_write, false);
  195.     if ( code >= 0 ) make_bool(op, code), code = 0;
  196.     return code;
  197. }
  198.  
  199. /* <num> cvi <int> */
  200. /* <string> cvi <int> */
  201. private int
  202. zcvi(register os_ptr op)
  203. {    float fval;
  204.     switch ( r_type(op) )
  205.        {
  206.     case t_integer:
  207.         return 0;
  208.     case t_real:
  209.         fval = op->value.realval;
  210.         break;
  211.     default:
  212.         return_op_typecheck(op);
  213.     case t_string:
  214.        {    ref str, token;
  215.         int code;
  216.         ref_assign(&str, op);
  217.         code = scan_string_token(&str, &token);
  218.         switch ( code )
  219.           {
  220.           case scan_EOF:            /* no tokens */
  221.           case scan_BOS:            /* not allowed */
  222.             code = gs_note_error(e_syntaxerror);
  223.           default:
  224.             if ( code < 0 )
  225.               return code;
  226.           }
  227.         switch ( r_type(&token) )
  228.           {
  229.           case t_integer:
  230.             *op = token;
  231.             return 0;
  232.           case t_real:
  233.             fval = token.value.realval;
  234.             break;
  235.           default:
  236.             return_error(e_typecheck);
  237.         }
  238.        }
  239.        }
  240.     /* Check if a real will fit into an integer value */
  241.     if ( !zcvi_possible(fval) )
  242.       return_error(e_rangecheck);
  243.     make_int(op, (long)fval);    /* truncates towards 0 */
  244.     return 0;
  245. }
  246.  
  247. /* <string> cvn <name> */
  248. private int
  249. zcvn(register os_ptr op)
  250. {    check_read_type(*op, t_string);
  251.     return name_from_string(op, op);
  252. }
  253.  
  254. /* <num> cvr <real> */
  255. /* <string> cvr <real> */
  256. private int
  257. zcvr(register os_ptr op)
  258. {    switch ( r_type(op) )
  259.        {
  260.     case t_integer:
  261.         make_real(op, op->value.intval);
  262.     case t_real:
  263.         return 0;
  264.     default:
  265.         return_op_typecheck(op);
  266.     case t_string:
  267.        {    ref str, token;
  268.         int code;
  269.         ref_assign(&str, op);
  270.         code = scan_string_token(&str, &token);
  271.         switch ( code )
  272.           {
  273.           case scan_EOF:            /* no tokens */
  274.           case scan_BOS:            /* not allowed */
  275.             code = gs_note_error(e_syntaxerror);
  276.           default:
  277.             if ( code < 0 )
  278.               return code;
  279.           }
  280.         switch ( r_type(&token) )
  281.           {
  282.           case t_integer:
  283.             make_real(op, token.value.intval);
  284.             return 0;
  285.           case t_real:
  286.             *op = token;
  287.             return 0;
  288.           default:
  289.             return_error(e_typecheck);
  290.           }
  291.        }
  292.        }
  293. }
  294.  
  295. /* <num> <radix_int> <string> cvrs <substring> */
  296. private int
  297. zcvrs(register os_ptr op)
  298. {    int radix;
  299.     check_type(op[-1], t_integer);
  300.     if ( op[-1].value.intval < 2 || op[-1].value.intval > 36 )
  301.         return_error(e_rangecheck);
  302.     radix = op[-1].value.intval;
  303.     check_write_type(*op, t_string);
  304.     if ( radix == 10 )
  305.        {    switch ( r_type(op - 2) )
  306.            {
  307.         case t_integer: case t_real:
  308.            {    int code = convert_to_string(op - 2, op);
  309.             if ( code < 0 ) return code;
  310.             pop(2);
  311.             return 0;
  312.            }
  313.         default:
  314.             return_op_typecheck(op - 2);
  315.            }
  316.        }
  317.     else
  318.        {    ulong ival;
  319.         byte digits[sizeof(ulong) * 8];
  320.         byte *endp = &digits[countof(digits)];
  321.         byte *dp = endp;
  322.         switch ( r_type(op - 2) )
  323.            {
  324.         case t_integer:
  325.             ival = (ulong)op[-2].value.intval;
  326.             break;
  327.         case t_real:
  328.            {    float fval = op[-2].value.realval;
  329.             if ( !real_can_be_int(fval) )
  330.                 return_error(e_rangecheck);
  331.             ival = (ulong)(long)fval;
  332.            }    break;
  333.         default:
  334.             return_op_typecheck(op - 2);
  335.            }
  336.         do
  337.            {    int dit = ival % radix;
  338.             *--dp = dit + (dit < 10 ? '0' : ('A' - 10));
  339.             ival /= radix;
  340.            }
  341.         while ( ival );
  342.         if ( endp - dp > r_size(op) ) return_error(e_rangecheck);
  343.         memcpy(op->value.bytes, dp, (uint)(endp - dp));
  344.         r_set_size(op, endp - dp);
  345.        }
  346.     op[-2] = *op;
  347.     pop(2);
  348.     return 0;
  349. }
  350.  
  351. /* <any> <string> cvs <substring> */
  352. private int
  353. zcvs(register os_ptr op)
  354. {    int code;
  355.     check_op(2);
  356.     check_write_type(*op, t_string);
  357.     code = convert_to_string(op - 1, op);
  358.     if ( code >= 0 ) pop(1);
  359.     return code;
  360. }
  361.  
  362. /* ------ Initialization procedure ------ */
  363.  
  364. BEGIN_OP_DEFS(ztype_op_defs) {
  365.     {"1cvi", zcvi},
  366.     {"1cvlit", zcvlit},
  367.     {"1cvn", zcvn},
  368.     {"1cvr", zcvr},
  369.     {"3cvrs", zcvrs},
  370.     {"2cvs", zcvs},
  371.     {"1cvx", zcvx},
  372.     {"1executeonly", zexecuteonly},
  373.     {"1noaccess", znoaccess},
  374.     {"1rcheck", zrcheck},
  375.     {"1readonly", zreadonly},
  376.     {"1type", ztype},
  377.     {"1wcheck", zwcheck},
  378.     {"1xcheck", zxcheck},
  379. END_OP_DEFS(ztype_init) }
  380.  
  381. /* ------ Internal routines ------ */
  382.  
  383. /* Test or modify the access of an object. */
  384. /* If modify = true, restrict to the selected access and return 0; */
  385. /* if modify = false, do not change the access, and return 1 */
  386. /* if the object had the access. */
  387. /* Return an error code if the object is not of appropriate type, */
  388. /* or if the object did not have the access already when modify=1. */
  389. private int near
  390. access_check(os_ptr op,
  391.     int access,                /* mask for attrs */
  392.     bool modify)                /* if true, reduce access */
  393. {    ref *aop;
  394.     switch ( r_type(op) )
  395.     {
  396.     case t_dictionary:
  397.         aop = dict_access_ref(op);
  398.         if ( modify )
  399.         {    if ( !r_has_attrs(aop, access) )
  400.                 return_error(e_invalidaccess);
  401.             ref_save(op, aop, "access_check(modify)");
  402.             r_clear_attrs(aop, a_all);
  403.             r_set_attrs(aop, access);
  404.             dict_set_top();
  405.             return 0;
  406.         }
  407.         break;
  408.     case t_array: case t_file: case t_string:
  409.     case t_mixedarray: case t_shortarray:
  410.     case t_astruct: case t_device: ;
  411.         if ( modify )
  412.         {    if ( !r_has_attrs(op, access) )
  413.               return_error(e_invalidaccess);
  414.             r_clear_attrs(op, a_all);
  415.             r_set_attrs(op, access);
  416.             return 0;
  417.         }
  418.         aop = op;
  419.         break;
  420.     default:
  421.         return_op_typecheck(op);
  422.     }
  423.     return (r_has_attrs(aop, access)? 1 : 0);
  424. }
  425.  
  426. /* Do all the work of cvs.  The destination has been checked, but not */
  427. /* the source.  This is a separate procedure so that */
  428. /* cvrs can use it when the radix is 10. */
  429. private int
  430. convert_to_string(os_ptr op1, os_ptr op)
  431. {    uint len;
  432.     const byte *pstr = 0;
  433.     int code = obj_cvs(op1, op->value.bytes, r_size(op), &len, &pstr);
  434.  
  435.     if ( code < 0 )
  436.       {    /* Some common downloaded error handlers assume that */
  437.         /* operator names don't exceed a certain fixed size. */
  438.         /* To work around this bit of bad design, we implement */
  439.         /* a special hack here: if we got a rangecheck, and */
  440.         /* the object is an operator whose name begins with */
  441.         /* %, ., or @, we just truncate the name. */
  442.         if ( code == e_rangecheck )
  443.           switch ( r_btype(op1) )
  444.             {
  445.             case t_oparray:
  446.             case t_operator:
  447.               if ( pstr != 0 )
  448.             switch ( *pstr )
  449.               {
  450.               case '%':
  451.               case '.':
  452.               case '@':
  453.                 len = r_size(op);
  454.                 memcpy(op->value.bytes, pstr, len);
  455.                 goto ok;
  456.               }
  457.             }
  458.         return code;
  459.       }
  460. ok:    *op1 = *op;
  461.     r_set_size(op1, len);
  462.     return 0;
  463. }
  464.